
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMLparser{
	
	private Document doc;
	private Data data;
		
	public XMLparser(InputStream input){
		//Create data object
		data = new Data();
		
		//Create new document with input
		createDocument(input);
		
		//Parse XML document for data
		parseXML();			
	}
	
	private void createDocument(InputStream input){
		File file = new File("C://Users/Henri/Desktop/Generator/output.xml");		
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		try {
			db = dbf.newDocumentBuilder();
		} catch (ParserConfigurationException e) {		
			e.printStackTrace();
		}		
		try {
			doc = db.parse(file);
		} catch (SAXException e) {			
			e.printStackTrace();
		} catch (IOException e) {		
			e.printStackTrace();
		}
		doc.getDocumentElement().normalize();		
	}
	
	/*
	 * Parse XML file and save the data in the data object 
	 */
	private void parseXML(){		
		
		NodeList nodeList = doc.getElementsByTagName("MEASUREMENT");
		
		for(int i =0; i<nodeList.getLength(); i++){
			Node nNode = nodeList.item(i);
			if(nNode.getNodeType() == Node.ELEMENT_NODE){
				Element eElement = (Element) nNode;	
				
				data.setStn(Integer.parseInt(getTagValue("STN", eElement)));				
				
				
				data.setDate(getTagValue("DATE", eElement));			
				
							
				data.setTime(getTagValue("TIME", eElement));
	
				data.setTemp(Double.parseDouble(getTagValue("TEMP", eElement)));
				data.setDewp(Double.parseDouble(getTagValue("DEWP", eElement)));
				data.setStp(Double.parseDouble(getTagValue("STP", eElement)));
				data.setSlp(Double.parseDouble(getTagValue("SLP", eElement)));
				data.setVisib(Double.parseDouble(getTagValue("VISIB", eElement)));
				data.setWdsp(Double.parseDouble(getTagValue("WDSP", eElement)));
				data.setPrcp(Double.parseDouble(getTagValue("PRCP", eElement)));
				data.setSndp(Double.parseDouble(getTagValue("SNDP", eElement)));
				data.setFrshtt(Integer.parseInt(getTagValue("FRSHTT", eElement)));
				data.setCldc(Double.parseDouble(getTagValue("CLDC", eElement)));
				data.setWnddir(Integer.parseInt(getTagValue("WNDDIR", eElement)));
				
				//System.out.println(data);
			}
		}
	}
		
	private String getTagValue(String sTag, Element eElement) {
		NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();		 
	    Node nValue = (Node) nlList.item(0);
	 
		return nValue.getNodeValue();
	}
	
	public Data getData(){
		return data;
	}


}